package com.edu.app.controller.core;

import com.alibaba.fastjson.JSON;
import com.edu.app.bean.core.SysContract;
import com.edu.app.bean.virtualbean.SelectedItems;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.model.customer.ContractModel;
import com.edu.app.service.sys.core.SysContractService;
import com.edu.app.service.sys.linkservices.SysContractLessonService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/contract")
@RestController
public class SysContractController {

    @Autowired
    private SysContractService sysContractService;

    @Autowired
    private SysContractLessonService sysContractLessonService;

    @RequestMapping(value = "")
    @RequiresAuthentication
    public ModelMap getContractList (){

        try{
            List<SysContract> contractList = sysContractService.getObjectList();
            return MsgUtil.retMsg(SysMessage.GET_CONTRACT_SUCCEED, StaticConstant.CONTRACT_LIST, contractList);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping(value = "/{id}")
    @RequiresAuthentication
    public ModelMap getContractById(@PathVariable Integer id){

        try{
            SysContract sysContract  = sysContractService.getObjectById(id);
            return MsgUtil.retMsg(SysMessage.GET_CONTRACT_SUCCEED, StaticConstant.CONTRACT, sysContract);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping("/pages")
    @RequiresAuthentication
    public ModelMap getContractByPage(@RequestParam Integer offset, @RequestParam Integer limit) {

        try {
            PageInfo pageInfo = sysContractService.findItemByPage(offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_USER_BY_PAGE_SUCCEED, "page_info", pageInfo);
        } catch (Exception e) {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }


    @Transactional
    @PostMapping(value = "")
    @RequiresAuthentication
    public ModelMap addContract(@RequestHeader("Authorization") String token,
                                @RequestBody SysContract sysContract,
                                @RequestParam String employeeList){

        try{
            String username = JWTUtil.getUsername(token);
            sysContract.initializeSystemColumns(username);
            List<SelectedItems> selectedItems = JSON.parseArray(employeeList, SelectedItems.class);

            //Contract employee, size as 1
            if(selectedItems.size() == 1){
                sysContract.setUser_id(selectedItems.get(0).getValue());
            }

            Integer r = sysContractService.add(sysContract);

            if(r > 0){
                return MsgUtil.retMsg(SysMessage.ADD_CONTRACT_SUCCEED, null, null);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.CONTRACT_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }


    @Transactional
    @PutMapping(value = "")
    @RequiresAuthentication
    public ModelMap updateContract (@RequestHeader("Authorization") String token, @RequestBody SysContract sysContract, @RequestParam String employeeList){

        String username = JWTUtil.getUsername(token);
        sysContract.initializeUpdateSystemColumns(username);

        SysContract oldContract = sysContractService.getObjectById(sysContract.getId());
        sysContract.setCreate_time(oldContract.getCreate_time());
        sysContract.setCreate_by(oldContract.getCreate_by());

        List<SelectedItems> selectedItems = JSON.parseArray(employeeList, SelectedItems.class);

        //Contract employee, size as 1
        if(selectedItems.size() == 1){
            sysContract.setUser_id(selectedItems.get(0).getValue());
        }

        Integer ret = sysContractService.update(sysContract);

        if(ret > 0 ){
            return MsgUtil.retMsg(SysMessage.UPDATE_CONTRACT_SUCCEED, null, null);
        }
        else{
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }

    @Transactional
    @DeleteMapping(value = "")
    @RequiresAuthentication
    public ModelMap deleteContract (@RequestParam Integer id){
        Integer ret;
        ret = sysContractService.delete(id);

        if( ret > 0)
        {
            return MsgUtil.retMsg(SysMessage.DELETE_CONTRACT_SUCCEED, null, null);
        }
        else
        {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }

    @GetMapping(value = "/getContractModelByCusId")
    @RequiresAuthentication
    public ModelMap getContractModelByCusId(@RequestParam Integer id){

        try{
            List<ContractModel> contractModelList  = sysContractService.getContractModelByCusId(id);
            return MsgUtil.retMsg(SysMessage.GET_CONTRACT_SUCCEED, StaticConstant.CONTRACT, contractModelList);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping(value = "/getContractModelByModelId")
    @RequiresAuthentication
    public ModelMap getContractModelByModelId(@RequestParam Integer id){

        try{
            ContractModel contractModel  = sysContractService.getContractModelByModelId(id);
            return MsgUtil.retMsg(SysMessage.GET_CONTRACT_SUCCEED, StaticConstant.CONTRACT, contractModel);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping(value = "/pages/getContractModelByCusIdAndPages")
    @RequiresAuthentication
    public ModelMap getContractModelByCusIdAndPages(@RequestParam("id") Integer id, @RequestParam("offset") Integer offset, @RequestParam("limit") Integer limit){

        try{
            PageInfo pageInfo  = sysContractService.getContractModelByCusIdAndPages(id, offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_USER_BY_PAGE_SUCCEED, "page_info", pageInfo);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }
}
